﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using jiaju.Areas.Back.Models.DAL;
using jiaju.Areas.Back.Models.DBModel;
using jiaju.Areas.Back.Models;
namespace jiaju.Areas.Back.Controllers
{
    [Authorize]
    public class BannerController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetBannerList(int page, int limit, string title)
        {
            List<Banner> datalist = BannerService.getBanner(page, limit, title);
            string sql = "select COUNT(*) from dbo.banner";
            int count = (int)DBHelp.ExcuteScalar(sql);
            Object obj = new
            {
                code = 0,
                msg = "请求成功",
                count = count,
                data = datalist

            };
            return Json(obj, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Add()
        {
             return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddDel(Banner ban)
        {
            int n = BannerService.addBanner(ban);
            if (n > 0)
            {
                return Json(new { success = true, msg = "新增成功" });
            }
            else
            {
                return Json(new { success = false, msg = "新增失败" });
            }

        }
        [HttpPost]
        public ActionResult UploadPhoto()
        {

           HttpPostedFileBase postFile = Request.Files["uplaodImg"];
            if (postFile != null)
            {
                string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png";
                string fullUploadPath = Server.MapPath("~/images");
                postFile.SaveAs(fullUploadPath + newFileName);
                return Json(new { success = true, path = "images" + newFileName, msg = "上传成功" });

            }
            else
            {
                return Json(new { success = false, path = "", msg = "上传失败" });
            }

        }
        public ActionResult Edit(string Id)
        {
            Banner ban = BannerService.GetById(Id);
            ViewBag.A = ban;
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditDel(Banner ban)
        {
            int m = BannerService.EditBanner(ban);
            if (m > 0)
            {
                return Json(new { success = true, msg = "编辑成功" });
            }
            else
            {
                return Json(new { success = false, msg = "编辑失败" });
            }
        }
        public ActionResult DelArt(String Id)
        {
            int n = BannerService.DelBanner(Id);
            if (n > 0)
            {
                return Json(new { success = true, msg = "删除成功" });
            }
            else
            {
                return Json(new { success = false, msg = "删除失败" });
            }
        }
    }
}